package com.ruoyi.framework.config;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.enums.BaseEnum;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;

import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

/**
 * Jackson序列化配置
 *
 * @author tqfeiyang
 * @since 2024/11/28 10:37
 */
@Configuration
public class JacksonConfig
{
    @Bean
    public ObjectMapper serializingObjectMapper() {
        JavaTimeModule module = new JavaTimeModule();
        module.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(Constants.DATETIME_FORMAT)));
        module.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern(Constants.DATE_FORMAT)));
        module.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern(Constants.TIME_FORMAT)));
        module.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(Constants.DATETIME_FORMAT)));
        module.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern(Constants.DATE_FORMAT)));
        module.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern(Constants.TIME_FORMAT)));

        // Long类型，序列化时转换成String
        //module.addSerializer(Long.class, ToStringSerializer.instance);

        // Long类型，反序列化时将String转换成Long
        //module.addDeserializer(Long.class, new NumberDeserializers.LongDeserializer(Long.class, null));

        // String类型，反序列化时去掉两边的空格
        module.addDeserializer(String.class, new StringWithoutSpaceDeserializer(String.class));

        // 枚举类型序列化
        //module.addSerializer(BaseEnum.class, new EnumsJsonSerializer());

        return Jackson2ObjectMapperBuilder.json().modules(module)
                .featuresToDisable(SerializationFeature.FAIL_ON_EMPTY_BEANS)
                .featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS).build();
    }

    public static class StringWithoutSpaceDeserializer extends StdDeserializer<String>
    {
        public StringWithoutSpaceDeserializer(Class<String> vc) {
            super(vc);
        }

        @Override
        public String deserialize(JsonParser p, DeserializationContext ctx) throws IOException
        {
            return p.getText() != null ? p.getText().trim() : null;
        }
    }

    public static class EnumsJsonSerializer extends JsonSerializer<BaseEnum>
    {
        @Override
        public void serialize(BaseEnum value, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException
        {
            jsonGenerator.writeStartObject();
            jsonGenerator.writeObjectField("name", value.toString());
            jsonGenerator.writeNumberField("value", value.getValue());
            jsonGenerator.writeStringField("description", value.getDescription());
            jsonGenerator.writeEndObject();
        }
    }
}
